<template>
  <XtxBread>
    <XtxBreadItem to="/">首页</XtxBreadItem>
    <XtxBreadItem v-if="category.top" :to="`/category/${category.top.id}`">{{ category.top.name }}</XtxBreadItem>
    <Transition name="fade-right">
      <XtxBreadItem v-if="category.sub" :key="category.sub.id">{{ category.sub.name }}</XtxBreadItem>
    </Transition>
  </XtxBread>
</template>

<script>

import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { computed } from 'vue'

export default {
  name: 'SubBread',
  components: {},
  setup() {
    // 通过计算属性从vuex获取顶级和二级目录信息
    // 数据对象: { top: {id, name}, sub:{id, name}}
    const store = useStore()
    const route = useRoute()

    const category = computed(() => {
      let cate = {}
      store.state.category.list.forEach(top => {
        if (top.children) {
          const sub = top.children.find(sub => sub.id === route.params.id)
          if (sub) {
            cate.top = {
              id: top.id,
              name: top.name
            }
            cate.sub = {
              id: sub.id,
              name: sub.name
            }
          }
        }
      })
      return cate
    })

    console.log(category.value)
    return {
      category
    }
  }
}
</script>

<style scoped>

</style>
